package com.cool.reggie.modules.employee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cool.reggie.modules.employee.entity.EmployeeEntity;
import com.cool.reggie.modules.employee.form.PageSearchForm;
import com.cool.reggie.modules.employee.service.EmployeeService;
import com.cool.reggie.utils.BaseContext;
import com.cool.reggie.utils.ResultData;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author 1950735817@qq.com
 * @since 2022-07-01
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    private final EmployeeService employeeService;

    @PostMapping("/login")
    public ResultData loginIn(HttpServletRequest request, @RequestBody EmployeeEntity employee) {


        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<EmployeeEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmployeeEntity::getUsername, employee.getUsername());
        EmployeeEntity user = employeeService.getOne(queryWrapper);

        if (user == null) {
            return ResultData.error("用户不存在");
        }

        if (!password.equals(user.getPassword())) {
            return ResultData.error("密码错误");
        }

        if (user.getStatus() == 0) {
            return ResultData.error("用户已锁定");
        }

        request.getSession().setAttribute("employee", user.getId());
        BaseContext.setCurrentId(user.getId());

        return ResultData.ok("登录成功", user);
    }


    @GetMapping("/page")
    public ResultData queryList(int page, int pageSize, String name) {

        // 构造分页
        Page<EmployeeEntity> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<EmployeeEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Strings.isNotBlank(name), EmployeeEntity::getName, name);

        employeeService.page(pageInfo, queryWrapper);

        PageSearchForm form = new PageSearchForm(page, pageSize, name);
        employeeService.queryPageData(form);

        return ResultData.ok(pageInfo);
    }

    @PostMapping("/logout")
    public ResultData logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return ResultData.ok("退出成功");
    }

    @PostMapping
    public ResultData saveEmployee(HttpServletRequest request, @RequestBody EmployeeEntity employee) {


        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

//        employee.setCreateTime(new Date());
//        employee.setUpdateTime(new Date());

//        Long userId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(userId);
//        employee.setUpdateUser(userId);
        boolean saveResult = employeeService.save(employee);


        return ResultData.ok("添加成功");

    }

    @PutMapping
    public ResultData forbiddenEmployee(HttpServletRequest request,@RequestBody EmployeeEntity employee) {

        EmployeeEntity employeeExit = employeeService.getById(employee.getId());
        if (Objects.isNull(employeeExit)) {
            return ResultData.error("该用户不存在");
        }

        employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());

        Long userId = (Long)request.getSession().getAttribute("employee");
        employee.setUpdateUser(userId);

        employeeService.updateById(employee);

        return ResultData.ok();
    }


    @GetMapping("/{id}")
    public ResultData updateEmployee(@PathVariable("id") String employeeId) {

        EmployeeEntity employee = employeeService.getById(employeeId);

        if (Objects.isNull(employee)) {
            return ResultData.error("该用户不存在");
        }
        return ResultData.ok(employee);
    }

}